package com.lagou.dao.impl;

import com.lagou.annotation.Autowired;
import com.lagou.annotation.Service;
import com.lagou.dao.AccountDao;
import com.lagou.pojo.Account;
import com.lagou.utils.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author: lcz
 * @date: Created in 18:32 2020/10/4
 * @description:
 */
@Service("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private ConnectionUtils connectionUtils;

    /**
     * @param cardNo 账号
     * @author: lcz
     * @date: Created in 11:04 2020/10/5
     * @description: 根据账号查询账户信息
     */
    @Override
    public Account queryAccountByCardNo(String cardNo) throws SQLException {
        //获取连接
        Connection con = connectionUtils.getCurrentThreadConn();
        String sql = "select * from account where card_no=?";
        PreparedStatement statement = con.prepareStatement(sql);
        statement.setString(1, cardNo);
        ResultSet resultSet = statement.executeQuery();

        Account account = new Account();
        while (resultSet.next()) {
            account.setId(resultSet.getInt("id"));
            account.setCardNo(resultSet.getString("card_no"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getInt("money"));
        }
        resultSet.close();
        statement.close();
        return account;
    }

    /**
     * @param account
     * @author: lcz
     * @date: Created in 11:05 2020/10/5
     * @description: 根据账号信息更新账户信息
     */
    @Override
    public int updateAccountByCardNo(Account account) throws SQLException {
        //获取连接
        Connection con = connectionUtils.getCurrentThreadConn();
        String sql = "update account set money=? where card_no=?";
        PreparedStatement statement = con.prepareStatement(sql);
        statement.setInt(1, account.getMoney());
        statement.setString(2, account.getCardNo());

        int rows = statement.executeUpdate();

        statement.close();
        statement.close();
        return rows;
    }
}
